package com.zxb.core.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.AllArgsConstructor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.Objects;

/**
 * @author zxb
 */
@AllArgsConstructor
public enum SexEnum implements BaseEnum {
    MAN("man", "男"),
    WOMAN("woman", "女");

    @EnumValue
    private final String value;
    private final String label;

    @Override
    public String getValue() {
        return value;
    }

    @Override
    public String getLabel() {
        return label;
    }

    public static SexEnum fromLabel(String label) {
        for (SexEnum one : SexEnum.values()) {
            if (Objects.equals(label, one.getLabel())) {
                return one;
            }
        }
        return SexEnum.MAN;
    }

    @Component
    public static class WebConverter implements Converter<String, SexEnum> {
        @Override
        public SexEnum convert(@Nonnull String source) {
            return SexEnum.fromLabel(source);
        }
    }
}
